package com.zx.mes.hyl.base.io;

import org.junit.Test;

import java.io.*;
import java.net.URL;

/**
 * io 流测试
 *
 * @author 华云龙
 * @date 2018-9-27
 */
public class IoTest {

    @Test
    public void test() throws IOException {
        URL resource = Thread.currentThread().getContextClassLoader().getResource("test.txt");
        System.out.println(resource);
        Reader reader = new FileReader(resource.getFile());
        char[] b=new char[1024];
        int len = 0;
        while ((len=reader.read(b))!=-1) {
            System.out.println("len:"+len);
            System.out.println(new String(b,0,len));
        }
//        Writer writer = new FileWriter("");
    }


    @Test
    public void test2() throws IOException {
        URL resource = Thread.currentThread().getContextClassLoader().getResource("test.txt");
        System.out.println(resource);
        Reader reader = new FileReader(resource.getFile());
        /// 如果b更大,更出现示例问题 :神一般的测试(空格)
        char[] b=new char[6];
        int len = 0;
        while ((len=reader.read(b))!=-1) {
            System.out.println("len:"+len);
            System.out.println(new String(b));
        }
//        Writer writer = new FileWriter("");
    }


    @Test
    public void test4() throws IOException {
        URL resource = Thread.currentThread().getContextClassLoader().getResource("test.txt");

        System.out.println(resource);
        Reader reader = new FileReader(resource.getFile());
        Writer writer = new FileWriter("C:\\Users\\Administrator\\Desktop\\test2.txt");
        char[] b=new char[1024];
        int len = 0;
        while ((len=reader.read(b))!=-1) {
            System.out.println("len:"+len);
            writer.write(b,0,len);
            System.out.println(new String(b,0,len));
        }

        reader.close();
        writer.close();

    }

    @Test
    public void test5() throws IOException {
        URL resource = Thread.currentThread().getContextClassLoader().getResource("test.txt");

        URL resource2 = IoTest.class.getResource("/");
        System.out.println(resource2.getPath());
        Reader reader = new FileReader(resource.getFile());
        File file = new File(resource2.getPath()+"\\test2.txt");

        Writer writer = new FileWriter(file);
        char[] b=new char[1024];
        int len = 0;
        while ((len=reader.read(b))!=-1) {
            System.out.println("len:"+len);
            writer.write(b,0,len);
            System.out.println(new String(b,0,len));
        }

        reader.close();
        writer.close();

    }

    /// 下面是Output  Input


}
